#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/22 0022 23:40
# @Author  : GXl
# @File    : apiv1uitls.py
# @Software: win10 Tensorflow1.13.1 python3.7

from flask import  jsonify
from utils import logutils



logger=logutils.getlogger(__file__)

def jsonerror(responseText ='error' ,status=None,statusText='error'):
    tmpdict=dict()
    tmpdict['success']=False
    tmpdict['result']=responseText
    tmpdict['statusText'] = statusText
    tmpdict['readyState']=4
    if status:
        tmpdict['status'] = status
    else:
        tmpdict['status'] = 500
    logger.error("errorinfo ={}".format(tmpdict ))
    return jsonify(tmpdict)

def jsonMsg(responseText):
    tmpdict=dict()
    tmpdict['success'] = True
    tmpdict['statusText'] = 'ok'
    tmpdict['result'] = responseText
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    return jsonify(tmpdict)

def jsonMsgWithSupplement(responseText,**kwargs):
    tmpdict=dict()
    tmpdict['success'] = True
    tmpdict['statusText'] = 'ok'
    tmpdict['result'] = responseText
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    for key,val in kwargs.items():
        tmpdict[key] = val
    return jsonify(tmpdict)

def jsonData(data, responseText='ok'):
    from app.models import DictModel
    tmpdict=dict()
    tmpdict['result'] = data.as_dict() if isinstance(data,DictModel) else data
    tmpdict['success']=True
    tmpdict['statusText']='ok'
    tmpdict['responseText']=responseText
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    logger.debug("successinfo ={}".format(tmpdict))
    return jsonify(tmpdict)

def jsonDataWithSupplement(data, **kwargs):
    from app.models import DictModel
    tmpdict=dict()
    tmpdict['result'] = data.as_dict() if isinstance(data,DictModel) else data
    tmpdict['success']=True
    tmpdict['statusText']='ok'
    tmpdict['responseText']='ok'
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    for key,val in kwargs.items():
        tmpdict[key] = val
    logger.debug("successinfo ={}".format(tmpdict))
    return jsonify(tmpdict)


def jsonDataList(modellist, responseText='ok'):
    from app.models import DictModel
    tmpdict=dict()
    tmplst=[]
    for item in modellist:
        tmplst.append(item.as_dict() if isinstance(item,DictModel) else item)
    tmpdict['result'] = tmplst
    tmpdict['success']=True
    tmpdict['statusText']='ok'
    tmpdict['responseText']=responseText
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    logger.debug("successinfo ={}".format(tmpdict ))
    return jsonify(tmpdict)

def jsonDataListWithSupplement(modellist, responseText='ok',**kwargs):
    from app.models import DictModel
    tmpdict=dict()
    tmplst=[]
    for item in modellist:
        tmplst.append(item.as_dict() if isinstance(item,DictModel) else item)
    tmpdict['result'] = tmplst
    tmpdict['success']=True
    tmpdict['statusText']='ok'
    tmpdict['responseText']=responseText
    tmpdict['readyState'] = 4
    tmpdict['status'] = 200
    for key,val in kwargs.items():
        tmpdict[key] = val
    logger.debug("successinfo ={}".format(tmpdict))
    return jsonify(tmpdict)